package com.blacktry.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.model.Menu;
import com.blacktry.entity.model.MenuRole;
import com.blacktry.service.MenuRoleService;
import com.blacktry.service.MenuService;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/system/config")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @OperLog(operModul = "用户管理-通过用户id查询菜单列表",operType = OprLogConst.SELECT,operDesc = "通过用户id查询菜单列表")
    @ApiOperation(value = "通过用户id查询菜单列表")
    @GetMapping("/menu")
    public List<Menu> getMenusByIdentityId() {
        return menuService.getMenusByAdminId();
    }

    @OperLog(operModul = "用户管理-查询所有菜单",operType = OprLogConst.SELECT,operDesc = "查询所有菜单")
    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/menus")
    public List<Menu> getMenus() {
        return menuService.getMenus();
    }

    @Autowired
    private MenuRoleService menuRoleService;

    @OperLog(operModul = "用户管理-根据角色id查询菜单",operType = OprLogConst.SELECT,operDesc = "根据角色id查询菜单")
    @ApiOperation(value = "根据角色id查询菜单")
    @GetMapping("/menus/{rid}")
    public List<Integer> getMenusByRid(@PathVariable("rid") Integer rid) {
        return menuRoleService.list(new QueryWrapper<MenuRole>().eq("role_id",rid)).stream().map(MenuRole::getMenuId).collect(Collectors.toList());
    }

    @OperLog(operModul = "用户管理-更新菜单",operType = OprLogConst.UPDATE,operDesc = "更新菜单")
    @ApiOperation(value = "更新菜单")
    @PutMapping("/updateMenuRole")
    public ApiResult<?> updateMenuRole(@RequestParam("rid") Integer rid, @RequestParam("mids") Integer[] mids) {
        return menuRoleService.updateMenuRole(rid,mids);
    }
}